1   /*
2    * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 5036680
27   * @summary Test the ObjectName.compareTo() method.
28   * @author Luis-Miguel Alventosa
29   * @run clean ComparatorTest
30   * @run build ComparatorTest
31   * @run main ComparatorTest
32   */
33  
34  import javax.management.*;
35  
36  public class ComparatorTest {
37  
38      private static final char LT = '<';
39      private static final char EQ = '=';
40      private static final char GT = '>';
41  
42      private static final String tests[][] = {
43          //
44          // domains
45          //
46          { String.valueOf(LT), ":k1=v1", "d:k1=v1" },
47          { String.valueOf(EQ), "d:k1=v1", "d:k1=v1" },
48          { String.valueOf(GT), "d2:k1=v1", "d1:k1=v1" },
49          //
50          // "type=" key property
51          //
52          { String.valueOf(GT), "d:type=a,k1=v1", "d:k1=v1" },
53          { String.valueOf(GT), "d:type=a,k1=v1", "d:type=" },
54          { String.valueOf(GT), "d:type=a,k1=v1", "d:type=,k1=v1" },
55          { String.valueOf(LT), "d:type=a,k1=v1", "d:type=b,k1=v1" },
56          { String.valueOf(LT), "d:type=a,k2=v2", "d:type=b,k1=v1" },
57          //
58          // canonical form
59          //
60          { String.valueOf(EQ), "d:k1=v1,k2=v2", "d:k2=v2,k1=v1" },
61          { String.valueOf(LT), "d:k1=v1,k2=v2", "d:k1=v1,k3=v3" },
62          { String.valueOf(LT), "d:k1=v1,k2=v2", "d:k2=v2,k1=v1,k3=v3" },
63          //
64          // wildcards
65          //
66          { String.valueOf(LT), "d:k1=v1", "d:k1=v1,*" },
67          { String.valueOf(GT), "d:k1=v1,k2=v2", "d:k1=v1,*" },
68          { String.valueOf(GT), "domain:k1=v1", "?:k1=v1" },
69          { String.valueOf(GT), "domain:k1=v1", "*:k1=v1" },
70          { String.valueOf(GT), "domain:k1=v1", "domai?:k1=v1" },
71          { String.valueOf(GT), "domain:k1=v1", "domai*:k1=v1" },
72          { String.valueOf(GT), "domain:k1=v1", "do?ain:k1=v1" },
73          { String.valueOf(GT), "domain:k1=v1", "do*ain:k1=v1" },
74      };
75  
76      private static boolean compare(char comparator, String on1, String on2) {
77          boolean ok = false;
78          System.out.println("Test " + on1 + " " + comparator + " " + on2);
79          try {
80              ObjectName o1 = ObjectName.getInstance(on1);
81              ObjectName o2 = ObjectName.getInstance(on2);
82              int result = o1.compareTo(o2);
83              switch (comparator) {
84                  case LT:
85                      if (result < 0)
86                          ok = true;
87                      break;
88                  case EQ:
89                      if (result == 0)
90                          ok = true;
91                      break;
92                  case GT:
93                      if (result > 0)
94                          ok = true;
95                      break;
96                  default:
97                      throw new IllegalArgumentException(
98                                "Test incorrect: case: " + comparator);
99              }
100         } catch (Exception e) {
101             ok = false;
102             System.out.println("Got Unexpected Exception = " + e.toString());
103         }
104         return ok;
105     }
106 
107     private static boolean lessThan(String on1, String on2) {
108         return compare(LT, on1, on2);
109     }
110 
111     private static boolean equalTo(String on1, String on2) {
112         return compare(EQ, on1, on2);
113     }
114 
115     private static boolean greaterThan(String on1, String on2) {
116         return compare(GT, on1, on2);
117     }
118 
119     private static int runTest(char comparator, String on1, String on2) {
120         System.out.println("----------------------------------------------");
121         boolean ok = false;
122         boolean lt = lessThan(on1, on2);
123         boolean eq = equalTo(on1, on2);
124         boolean gt = greaterThan(on1, on2);
125         switch (comparator) {
126             case LT:
127                 ok = lt && !eq && !gt;
128                 System.out.println("Comparison result: LessThan");
129                 break;
130             case EQ:
131                 ok = !lt && eq && !gt;
132                 System.out.println("Comparison result: EqualTo");
133                 break;
134             case GT:
135                 ok = !lt && !eq && gt;
136                 System.out.println("Comparison result: GreaterThan");
137                 break;
138             default:
139                 throw new IllegalArgumentException(
140                           "Test incorrect: case: " + comparator);
141         }
142         if (ok)
143             System.out.println("Test passed!");
144         else
145             System.out.println("Test failed!");
146         System.out.println("----------------------------------------------");
147         return ok ? 0 : 1;
148     }
149 
150     public static void main(String[] args) throws Exception {
151 
152         int error = 0;
153 
154         // Check null values
155         //
156         System.out.println("----------------------------------------------");
157         System.out.println("Test ObjectName.compareTo(null)");
158         try {
159             new ObjectName("d:k=v").compareTo(null);
160             error++;
161             System.out.println("Didn't get expected NullPointerException!");
162             System.out.println("Test failed!");
163         } catch (NullPointerException e) {
164             System.out.println("Got expected exception = " + e.toString());
165             System.out.println("Test passed!");
166         } catch (Exception e) {
167             error++;
168             System.out.println("Got unexpected exception = " + e.toString());
169             System.out.println("Test failed!");
170         }
171         System.out.println("----------------------------------------------");
172 
173         // Compare ObjectNames
174         //
175         for (int i = 0; i < tests.length; i++)
176             error += runTest(tests[i][0].charAt(0), tests[i][1], tests[i][2]);
177 
178         if (error > 0) {
179             final String msg = "Test FAILED! Got " + error + " error(s)";
180             System.out.println(msg);
181             throw new IllegalArgumentException(msg);
182         } else {
183             System.out.println("Test PASSED!");
184         }
185     }
186 }